<?php
/**
 * 裁剪
 *
 * @copyright  Copyright (c) 2007-2016 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */

use Shopnc\Tpl;

defined('InShopNC') or exit('Access Invalid!');

class cutControl extends BaseCircleControl {
    public function __construct(){
        Language::read('cut');
        parent::__construct();
    }
    /**
     * 图片上传
     *
     */
    public function pic_uploadOp(){
        if (chksubmit()){
            //上传图片
            $upload = new UploadFile();
            $upload->set('thumb_width', 500);
            $upload->set('thumb_height',499);
            $upload->set('thumb_ext','_small');
            $upload->set('max_size',C('image_max_filesize')?C('image_max_filesize'):1024);
            $upload->set('ifremove',true);
            $upload->set('default_dir',$_GET['uploadpath']);

            if (!empty($_FILES['c_img']['tmp_name'])){
                $result = $upload->upfile('c_img');
                if ($result){
                    exit(json_encode(array('status'=>1,'url'=>UPLOAD_SITE_URL.'/'.$_GET['uploadpath'].'/'.$upload->thumb_image)));
                }else {
                    exit(json_encode(array('status'=>0,'msg'=>$upload->error)));
                }
            }
        }
    }

    /**
     * 图片裁剪
     *
     */
    public function pic_cutOp(){

        if (chksubmit()){
            $thumb_width = $_POST['x'];
            $x1 = $_POST["x1"];
            $y1 = $_POST["y1"];
            $x2 = $_POST["x2"];
            $y2 = $_POST["y2"];
            $w = $_POST["w"];
            $h = $_POST["h"];
            $scale = $thumb_width/$w;
            $src = str_ireplace(UPLOAD_SITE_URL,BASE_UPLOAD_PATH,$_POST['url']);
            if (strpos($src, '..') !== false || strpos($src, BASE_UPLOAD_PATH) !== 0) {
                exit();
            }
            if (!empty($_POST['filename'])){
                $save_file2 = BASE_UPLOAD_PATH.'/'.$_POST['filename'];
            }else{
                $save_file2 = str_replace('_small.','_sm.',$src);
            }

            \Shopnc\Lib::imager()->createImageFromPath($src)
                ->clip($x1, $y1, $w, $h, $scale)
                ->save($save_file2);

            @unlink($src);
            $pathinfo = pathinfo($save_file2);
            exit($pathinfo['basename']);
        }
        $save_file = str_ireplace(UPLOAD_SITE_URL,BASE_UPLOAD_PATH,$_GET['url']);
        $_GET['x'] = (intval($_GET['x'])>50 && $_GET['x']<400) ? $_GET['x'] : 200;
        $_GET['y'] = (intval($_GET['y'])>50 && $_GET['y']<400) ? $_GET['y'] : 200;
        $_GET['resize'] = $_GET['resize'] == '0' ? '0' : '1';

        $image = \Shopnc\Lib::imager()->createImageFromPath($save_file);
        Tpl::output('height', $image->height);
        Tpl::output('width', $image->width);

        Tpl::showpage('cut','null_layout');
    }

//  public function pic_viewOp(){
//      header('Cache-Control:no-cache,must-revalidate');
//      header('Pragma:no-cache');
//      header('Content-type: image/jpeg');
//      echo file_get_contents($_GET['url']);
//  }
}
